﻿// https://leetcode-cn.com/problems/count-complete-tree-nodes/
// 222. 完全二叉树的节点个数

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode *left = root->left;
        TreeNode *right = root->right;

        int left_depth = 0, right_depth = 0;

        while (left) {
            left = left->left;
            left_depth++;
        }
        while (right) {
            right = right->right;
            right_depth++;
        }
        if (left_depth == right_depth)
            return (2 << left_depth) - 1;
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

int main() {
    Solution obj = Solution();
}